package b3_priorityqueue;

import java.util.Arrays;
import java.util.Queue;

/**
 * 测试优先级队列，实现Comparable接口
 */
public class TestPriorityQueue_2 {
    public static void main(String[] args) {

        Student stu1 = new Student("小黑", 19);
        Student stu2 = new Student("小白", 20);
        Student stu3 = new Student("小绿", 18);

        //利用数组排序
        Student[] stus = {stu1, stu2, stu3};
        Arrays.sort(stus);
        System.out.println(Arrays.toString(stus));

        //优先级队列排序
        Queue<Student> queue = new java.util.PriorityQueue<>();
        //入队
        queue.offer(stu1);
        queue.offer(stu2);
        queue.offer(stu3);
        //出队
        stus[0] = queue.poll();
        stus[1] = queue.poll();
        stus[2] = queue.poll();
        System.out.println(Arrays.toString(stus));
    }
}

//自定义Student类实现Comparable接口，具备了比较大小的功能
class Student implements Comparable<Student>{

    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        // 结果 > 0，说明 this.age > o.age
        return this.age - o.age;
    }
}
